package com.example.tatil.controller;

import com.example.tatil.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {


    @PostMapping("/upload")
    public Result upload(String username, Integer age, MultipartFile image) throws Exception {

        log.info("文件上传，参数：{},{},{}",username,age,image);
       //获取文件原始名
        String originalFilename = image.getOriginalFilename();

        int i = 0;
        if (originalFilename != null) {
            i = originalFilename.lastIndexOf(".");
        }
        String substring = null;
        if (originalFilename != null) {
            substring = originalFilename.substring(i);
        }
        String newFile = UUID.randomUUID().toString()+ substring;
        log.info("新的文件名:{}",newFile);
        //将接收到的文件存储到服务器的磁盘目录中
        //构造唯一的文件名（不能重复） uuid(通用唯一识别码)


        image.transferTo(new File("D:\\DevekopApp\\image\\" + newFile));

        return Result.success();
    }
}
